package com.mingtang.online.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.mingtang.online.R;
import com.mingtang.online.changliang.Const;
import com.mingtang.online.http.MyOkhttp;
import com.mingtang.online.http.callback.StringNoDialogCallback;
import com.mingtang.online.http.request.BaseRequest;
import com.mingtang.online.model.IndexClassfication;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class HomeFragment extends BaseFragment implements OnTabSelectListener {
    @BindView(R.id.layout_renwu)
    LinearLayout layoutRenwu;
    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.layout_notice)
    LinearLayout layoutNotice;
    @BindView(R.id.tl_4)
    SlidingTabLayout tl4;
    @BindView(R.id.vp)
    ViewPager vp;
    Unbinder unbinder;
    private Context mContext = getActivity();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "推荐", "男装", "数码家电"
            , "运动", "鞋包", "美食", "工具资源"
    };
    private MyPagerAdapter mAdapter;

    private List<IndexClassfication.ContentBean> contentBeanList = new ArrayList<>();

    private List<String> list_title = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        unbinder = ButterKnife.bind(this, view);

        getData();
        return view;
    }

    public void getData() {
        //首页分类列表	http://www.50210.com.cn/app/index.php?i=3&c=entry&do=getfztype&m=mingtang

//        http://www.50210.com.cn/app/index.php?i=3&c=entry&type=11&do=newcat&m=tiger_newhu


        MyOkhttp.post(Const.MODEL_URL)
                .tag(this)
                .params("i", "3")
                .params("c", "entry")
                .params("do", "getfztype")
                .params("m", "mingtang")
                .execute(new StringNoDialogCallback() {


                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);


                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        IndexClassfication indexClassfication = new IndexClassfication();
                        Gson gson = new Gson();
                        indexClassfication = gson.fromJson(s, IndexClassfication.class);
                        contentBeanList = indexClassfication.getContent();
                        List<String> strings=new ArrayList<String>();
                        strings.add("0");
                        list_title.add("全部");
                        for (int i = 0; i < contentBeanList.size(); i++) {
                            list_title.add(contentBeanList.get(i).getTitle());
                            strings.add(contentBeanList.get(i).getId());
                        }

                        for (String title : strings) {
                            mFragments.add(SimpleCardFragment.getInstance(title));
                        }
                        mAdapter = new MyPagerAdapter(getFragmentManager());
                        vp.setAdapter(mAdapter);
                        tl4.setViewPager(vp);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);

                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);

                    }
                });

    }


    @Override
    public void onTabSelect(int position) {
        Toast.makeText(mContext, "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabReselect(int position) {
        Toast.makeText(mContext, "onTabReselect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.layout_renwu, R.id.img_search, R.id.imageView, R.id.textView, R.id.layout_notice, R.id.tl_4, R.id.vp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_renwu:
                break;
            case R.id.img_search:
                break;
            case R.id.imageView:
                break;
            case R.id.textView:
                break;
            case R.id.layout_notice:
                break;
            case R.id.tl_4:
                break;
            case R.id.vp:
                break;
        }
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
//            return mTitles[position];
            return list_title.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
